<?php
namespace app\app\controller\v1;
use think\Db;
use think\Request;

class TeamSpreadController extends BaseController
{
	
    /**
     * 显示资源列表
     * 团队成员列表
     * @return \think\Response
     */
    public function index()
    {
    	$request = Request::instance();
    	$header  = $request->header();
    	$user_id = getUserId($header['openid']);
    	//搜索条件
    	$where = '';
    	if(input('get.name')){
    		$where .= 'and c.name like "%'.input('get.name').'%" ';
    	}
    	if(input('get.position')){
			$where .= 'and c.position like "%'.input('get.position').'%" ';
    	}
    	if(input('get.company_name')){
			$where .= 'and c.company_name like "%'.input('get.company_name').'%" ';
    	}
    	$id = intval(input('get.id'));
    	$team_spread = Db::query('select a.user_id,a.team_id,c.id,c.name,c.position,c.company_name,concat("'.config('IMG_HEAD').',c.portrait") as card_portrait,b.username,concat("'.config('IMG_HEAD').',b.portrait") as user_portrait from hmp_team_spread a left join hmp_users b on a.user_id=b.id left join hmp_card c on a.user_id=c.user_id where a.team_id='.$id.' and b.status=1 '.$where);
    	return hmp(200,'success',['data'=>$team_spread]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

    }

    /**
     * 保存新建的资源
     * 点赞
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {

    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {

    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update($id)
    {

    }

    /**
     * 删除指定资源
     * 退出团队
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
    	$request = Request::instance();
    	$header  = $request->header();
    	$id = intval(input('id'));
    	$user_id = getUserId($header['openid']);
    	$team = Db::name('team')->where('id','eq',$id)->find();
    	if($user_id && $team['user_id'] == $user_id){
    		//群主无法退出
    		return hmp(402,'群主无法退出');
    	}else{
    		Db::name('team_spread')->where('team_id','eq',$id)->where('user_id','eq',$user_id)->delete();
    		Db::query('update hmp_team set total = total - 1 where id ='.$id);
    		return hmp(200,'退出成功');
    	}
    }

}